<?php
/**
* 图表Xml基类
*
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   SVN: $Id$
 */
namespace Project\Apps\Ecga\Library\FusionCharts;

use \Exception;
/**
* 图表Xml基类
*
 * PHP version 5
 *
 * @package   Ecga
 * @author    Farmer.Li <none@none.com>
 * @copyright 2010 Xingchangxinda Inc.
 * @version   Release: <0.2>
 */
abstract class ChartXml
{
	/**
	 * data
	 */
	protected $data = array();
	
	/**
	 * 构造函数
	 * 
	 * @param string $caption 图表说明
	 * 
	 * @return void
	 */
	public function __construct($caption)
	{
		$this->data = array(
			'tagName' => 'chart',
			'__child' => array(),
			'__attr' => array(
				'caption' => $caption,
				'baseFont' => '宋体',
				'baseFontSize' => 12,
				'outCnvBaseFontSize' => 12,
				'formatNumber' => 1,
				'formatNumberScale' => 0,
				'decimalPrecision' => 2
			)
		);
	}
	
	/**
	 * 设置图表样式
	 * 在<chart '位置'>...</chart>插入
	 * 
	 * @param array $style 样式属性
	 * 
	 * @return void
	 */
	public function setStyle($style)
	{
		if (!is_array($style)) {
			throw new Exception('Style Format Is A Array!');
		}
		foreach ($style as $key => $value) {
			$this->data['__attr'][strval($key)] = strval($value);
		}
	}
	
	/**
	 * 格式数组转换成Xml
	 * 数组格式:
	 * array(
	 * 		0 => array(
	 *			'tagName' => '',
	 * 			'__attr' => array(
	 * 				'key1' => 'value1',
	 * 				'key2' => 'value2',
	 * 				//...
	 * 			),
	 * 			'__child' => array(
	 * 				//....
	 *			),
	 *			'value' => ''
	 * 		),
	 * 		//....
	 * );
	 * 
	 * @param array $arrayXml 格式化的数组
	 * 
	 * @return string
	 */
	protected function arrayToXml($arrayXml)
	{
		$result = '';
		if ($arrayXml == array()) {
			return $result;
		}
		foreach ($arrayXml as $element) {
			if (isset($element['__attr'])) {
				$attrArr = array();
				foreach ($element['__attr'] as $key => $value) {
					$attrArr[] = sprintf("%s=\"%s\"", $key, $value);
				}
				$element['__attrToString'] = implode(' ', $attrArr);
			}
			if (isset($element['value'])) {
				$result .= sprintf(
					"<%s %s>%s</%s>",
					$element['tagName'],
					$element['__attrToString'],
					$element['value'],
					$element['tagName']
				);
			} elseif (isset($element['__child'])) {
				$child = $this->arrayToXml($element['__child']);
				$result .= sprintf(
					"<%s %s>%s</%s>",
					$element['tagName'],
					$element['__attrToString'],
					$child,
					$element['tagName']
				);
			} else {
				$result .= sprintf("<%s %s />", $element['tagName'], $element['__attrToString']);
			}
		}
		//$result = mb_convert_encoding($result, 'utf-8', 'gbk');
		return pack("C3", 0xef, 0xbb, 0xbf) . $result;
	}
	
	/**
	 * 过滤
	 * 
	 * @param mixed $data 要过滤的数据
	 * 
	 * @return mixed
	 */
	public function filter($data)
	{
	    htmlspecialchars($string, ENT_QUOTES);
		$finds = array('\'', '"', '<', '>', '&');
		$replace = array("%26apos;", "&quot;", "&lt;", "&gt;", '&amp;');
		$result = array();
		if (is_string($data) || is_numeric($data) || is_null($data)) {
			//return str_replace($finds, $replace, $data);
			return htmlspecialchars($data, ENT_QUOTES);
		} elseif (is_array($data)) {
			foreach ($data as $key => $value) {
				//$key = str_replace($finds, $replace, $key);
				$key = htmlspecialchars($key, ENT_QUOTES);
				
				if (is_string($data) || is_numeric($data)) {
					//return str_replace($finds, $replace, $value);
					return htmlspecialchars($value, ENT_QUOTES);
				} else {
					$value = $this->filter($value);
				}
				$result[$key] = $value;
			}
		} else {
			throw new Exception('未被允许的数据类型');
		}
		return $result;
	}
	
	/**
	 * 输出xml
	 * 
	 * @return string
	 */
	public function put()
	{
	    $this->format();
		$data = $this->filter($this->data);
		return $this->arrayToXml(array($data));
	}
	
	/**
	 * 格式化数组
	 * 
	 * @return void
	 */
	protected function format()
	{
		/**
		 * @todo 格式化 $this->data
		 */
	}
	
	/**
	 * 生成一个随机颜色
	 * 
	 * @return colorString
	 */
	protected function randColor()
	{
		$color = '';
		for ($tmp = 0; $tmp < 6; $tmp++) {
			$color .= dechex(rand(0, 15));
		}
		return $color;
	}
}
?>